package e.odbo.data.generator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PasswordGenerator extends RoundStringGenerator{
    Logger logger= LoggerFactory.getLogger(PasswordGenerator.class);

    boolean symbol=false;

    public PasswordGenerator(int lineLength) {
        this(lineLength,false);
    }

    public PasswordGenerator(int lineLength,boolean symbol) {
        super(lineLength>4?lineLength:4,symbol);
        this.symbol=symbol;
    }

    public PasswordGenerator(int lineLength,String extStr,boolean symbol) {
        super(lineLength>4?lineLength:4,extStr,symbol);
        this.symbol=symbol;
    }

    @Override
    public String generator() {
        String pwd= super.generator();
        int count=0;
        while (!validPwd(pwd)) {
            pwd = super.generator();
            count++;
            if(count>100) {
                logger.debug("PasswordGenerator while up loop:{}",count);
                break;
            }
        }
        return pwd;
    }

    /**
     * 校验密码
     * 1、长度不小于6位
     * 2、必须以字母开头
     * 3、必须包含特殊字符
     * 4、必须包含数字
     * @param pwd
     * @return
     */
    public  boolean validPwd(String pwd){
        if(symbol&&pwd.matches("^[a-zA-z](.*)") && pwd.matches("(.*)[-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]+(.*)")&& pwd.matches("(.*)\\d+(.*)"))
            return true;
        if(pwd.matches("^[a-zA-z](.*)")  && pwd.matches("(.*)\\d+(.*)")){
            return true;
        }
        return false;
    }

    @Override
    public String getName() {
        return "Password";
    }
}
